package com.woniu.controller;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Member;
import com.woniu.service.IDetailedOrderService;
import com.woniu.service.IOrderService;
import com.woniu.vo.ClientGoodsOrderVo;
import com.woniu.vo.OrderDetailOrderVo;
import com.woniu.vo.PaymentOrderPageVo;
import com.woniu.vo.SettlementConfirmationOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: qiupeng
 * @Date: 2022/10/18
 * @Time: 14:07
 * @Version:
 */
@RestController
@RequestMapping("/ClientSpringOrder")
@Api(tags = "用户端商品订单管理管理")
public class ClientGoodsOrderManagement extends BaseController {


    @Autowired
    private IOrderService iOrderService;





    @PostMapping
    @ApiOperation("生成商品购物订单")
    public Result GenerateGoodsOrder(@RequestBody SettlementConfirmationOrderVo settlementConfirmationOrderVo, HttpSession session){
        Member member = (Member)session.getAttribute("member");
        BigDecimal discountBig = (BigDecimal)session.getAttribute("discount");
        System.err.println(discountBig);
        double discount = Double.parseDouble(String.valueOf(discountBig));
        ClientGoodsOrderVo goodsOrderVo =  iOrderService.GenerateGoodsOrder( settlementConfirmationOrderVo,member.getId(),discount);
       return  toDataResult(goodsOrderVo);



    }

    @PostMapping("/paymentGoodsOrder")
    @ApiOperation("支付商品购物订单")
    public Result paymentGoodsOrder(  @RequestBody ClientGoodsOrderVo clientGoodsOrderVo,HttpSession session){
        Member member =(Member) session.getAttribute("member");
        PaymentOrderPageVo orderDetailOrderVo =  iOrderService.paymentGoodsOrder(clientGoodsOrderVo,member.getId());
         return toDataResult(orderDetailOrderVo);
    }






    /**
     *  string转map
     * @param param
     * @return
     */
    public Map<String, Object> getValue(String param) {
        Map<String, Object> map = new HashMap<>();
        String str = "";
        String key = "";
        Object value = "";
        char[] charList = param.toCharArray();
        boolean valueBegin = false;
        for (int i = 0; i < charList.length; i++) {
            char c = charList[i];
            if (c == '{') {
                if (valueBegin == true) {
                    value = getValue(param.substring(i, param.length()));
                    i = param.indexOf('}', i) + 1;
                    map.put(key, value);
                }
            } else if (c == '=') {
                valueBegin = true;
                key = str;
                str = "";
            } else if (c == ',') {
                valueBegin = false;
                value = str;
                str = "";
                map.put(key, value);
            } else if (c == '}') {
                if (str != "") {
                    value = str;
                }
                map.put(key, value);
                return map;
            } else if (c != ' ') {
                str += c;
            }
        }
        return map;
    }


}
